<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="60%" append-to-body custom-class="import-dialog">
      <div class="app-container">
        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="table">
          <el-table-column label="模板名称" align="center" prop="templateName"/>
          <el-table-column label="标题" align="center" prop="messageHeader"/>
          <el-table-column label="内容" align="center" prop="messageContent"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button @click="handleSelect(scope.row)" :type="ids.includes(scope.row.publicMessageId) ? 'primary' : ''">
                {{ ids.includes(scope.row.publicMessageId) ? '已选择' : '选择' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
      <div slot="footer" class="dialog-footer" v-if="isMultiple">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { debounce } from '@/utils'
import {listPublicMessage} from "@/api/messageCenter/publicMessage";

export default {
  props: {
    title: {
      default: '选择公共模板'
    },
    show: {
      default: false
    },
    selectedData: {
      default: () => []
    },
    isMultiple: {
      default: false
    }
  },
  dicts: [],
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: undefined,
        messageHeader: undefined,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      // 选中列表
      selectedList: []
    }
  },
  computed: {
    visible: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  watch: {
    show: {
      handler(newVal) {
        if (newVal) {
          this.init()
        }
      }
    }
  },
  methods: {
    init() {
      this.reset()
      this.getList()
    },

    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        templateName: undefined,
        messageHeader: undefined,
      }

      this.ids = this.selectedData.map(item => item.publicMessageId)
      this.selectedList = [...this.selectedData]

      this.resetForm('queryForm')
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      // this.dateRange = [];
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 查询房间列表 */
    getList() {
      this.loading = true
      listPublicMessage(this.queryParams).then((response) => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleSelectionChange() {
    },
    // 选择房间
    handleSelect(item) {
      if (this.isMultiple) {
        if (this.ids.includes(item.publicMessageId)) {
          this.ids.splice(this.ids.indexOf(item.publicMessageId), 1)
          this.selectedList.splice(this.selectedList.indexOf(item), 1)
        } else {
          this.ids.push(item.publicMessageId)
          this.selectedList.push(item)
        }
      } else {
        this.ids = [item.publicMessageId]
        this.selectedList = [item]
        this.$emit('success', this.selectedList)
        this.close()
      }
    },
    /** 确定 */
    submit: debounce(function() {
      if (this.isMultiple) {
        if (this.ids.length === 0) {
          this.$message.error('请选择')
          return
        }
        this.$emit('success', this.selectedList)
        this.close()
      }

    }, 500, true),
    close() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/dialog.scss";
</style>
